﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DungeonCraft.DataModel.Abilities.Interfaces;
using Microsoft.Xna.Framework;
using DungeonCraft.DataModel.Tools;
using DungeonCraft.DataModel.Interfaces;

namespace DungeonCraft.DataModel.Abilities
{
    public class MagicMissile : AManaUsingAbility, IAbility, IDamageRectangle
    {
        private ICallbackTimer timer;
        private double x;
        private double y;
        private double rotation;
        private double vX;
        private double vY;
        private double speed;
        private Player player;
        private HitDetector hitDetector;

        public MagicMissile(ICallbackTimer timer, HitDetector hitDetector)
        {
            this.timer = timer;
            speed = 5;
            this.hitDetector = hitDetector;
        }

        public override string Execute(DataModel.Interfaces.ILevel level, Player player, IGuard guard, Location mouseLocation)
        {
            //calculate vector
            if (!guard.Check(level, player, this))
                return "Could not cast Magicmissile";
            player.CurrentAbility = this;
            this.player = player;
            x = Convert.ToDouble(player.Location.X);
            y = Convert.ToDouble(player.Location.Y);
            rotation = Math.Atan2(Convert.ToDouble(mouseLocation.Y - player.Location.Y), Convert.ToDouble(mouseLocation.X - player.Location.X));
            vX = speed * Math.Cos(rotation);
            vY = speed * Math.Sin(rotation);
            timer.Reset();
            timer.TickCallBack = Move;
            timer.EndCallBack = Destroy;
            timer.tickTime = 50;
            timer.NumberOfTicks = 20;
            timer.Start();
            return "Cast magic missile";
        }

        private double Normalise(double degrees)
        {
            double retval = degrees % 360;
            if (retval < 0)
                retval += 360;
            return retval;
        }


        public void Move()
        {

            x += vX;
            y += vY;
            foreach (Player player in hitDetector.detectHits(this))
                player.Kill();
        }

        public void Destroy()
        {
            player.CurrentAbility = null;
            player.StartCooldown(new CallbackTimer(50, 20));
        }

        public override bool Drawable()
        {
            return true;
        }


        public int X
        {
            get
            {
                return Convert.ToInt32(x);
            }
            set
            {
                x = value;
            }
        }

        public double VX
        {
            get
            {
                return Convert.ToInt32(vX);
            }
            set
            {
                vX = value;
            }
        }

        public double VY
        {
            get
            {
                return Convert.ToInt32(vY);
            }
            set
            {
                vY = value;
            }
        }

        public int Y
        {
            get
            {
                return Convert.ToInt32(y);
            }

            set
            {
                y = value;
            }
        }


        public double Rotation
        {
            get
            {
                return rotation;
            }
            set
            {
                rotation = value;
            }
        }

        public Rectangle getDamageField()
        {
            return new Rectangle(X, Y, 10, 15);
        }

    }
}
